/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaAgregarCanal.java
 *
 * Created on 17/12/2008, 23:19:16
 */

package vista;

//import datos.CanalSql;
import dominio.Canal;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import vista.util.VentanaNuevoCanalUtil;

/**
 *
 * @author Marcelo
 */
public class VentanaAgregarCanal extends javax.swing.JFrame
{
    private Collection canales = new ArrayList();
    private Collection nuevosCanales = new ArrayList();
    private VentanaNuevoCanalUtil util = new VentanaNuevoCanalUtil();
    private VentanaNuevoPerfil nuevoPerfil = new VentanaNuevoPerfil();


    /** Creates new form VentanaAgregarCanal */
    public VentanaAgregarCanal() {
        initComponents();
        this.inicializar();
        //llll

    }

    public VentanaAgregarCanal(VentanaNuevoPerfil nuevoPerfil)
    {
        initComponents();
        this.inicializar();
        this.nuevoPerfil = nuevoPerfil;
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableCanales = new javax.swing.JTable();
        jButtonAceptar = new javax.swing.JButton();
        jButtonNuevo = new javax.swing.JButton();
        jButtonSalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Agregar Canal");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Canales"));

        jTableCanales.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nombre", "Frec.", "Selec."
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableCanales.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableCanalesMouseClicked(evt);
            }
        });
        jTableCanales.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTableCanalesKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTableCanales);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
        );

        jButtonAceptar.setText("Agregar");
        jButtonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAceptarActionPerformed(evt);
            }
        });

        jButtonNuevo.setText("Nuevo");
        jButtonNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNuevoActionPerformed(evt);
            }
        });

        jButtonSalir.setText("Salir");
        jButtonSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(jButtonAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(68, 68, 68)
                .addComponent(jButtonNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 71, Short.MAX_VALUE)
                .addComponent(jButtonSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(50, 50, 50))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAceptar)
                    .addComponent(jButtonSalir)
                    .addComponent(jButtonNuevo))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTableCanalesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableCanalesMouseClicked
        // TODO add your handling code here:
//        this.mostrarComentario(this.jTableCanales.getSelectedRow());
}//GEN-LAST:event_jTableCanalesMouseClicked

    private void jTableCanalesKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTableCanalesKeyReleased
        // TODO add your handling code here:
//        if(evt.getKeyCode() != 10)
//            this.vFila = this.jTableCanales.getSelectedRow();
//        else
//            this.modificarRegistro(this.vFila);
}//GEN-LAST:event_jTableCanalesKeyReleased

    private void jButtonSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSalirActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_jButtonSalirActionPerformed

    private void jButtonNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNuevoActionPerformed
        // TODO add your handling code here:
        VentanaNuevoCanal nuevoCanal = new VentanaNuevoCanal(this);
        nuevoCanal.setVisible(true);
    }//GEN-LAST:event_jButtonNuevoActionPerformed

    private void jButtonAceptarActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonAceptarActionPerformed
    {//GEN-HEADEREND:event_jButtonAceptarActionPerformed
        // TODO add your handling code here:
        this.agregarCanal();
        this.dispose();
    }//GEN-LAST:event_jButtonAceptarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentanaAgregarCanal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAceptar;
    private javax.swing.JButton jButtonNuevo;
    private javax.swing.JButton jButtonSalir;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableCanales;
    // End of variables declaration//GEN-END:variables

    /////////////////// INICIALIZACION DE FORMULARIO ////////////////////
    private void inicializar()
    {
        this.centrar();
        this.ajustarTamanioColumna();
        this.limpiar();
        this.llenarTabla();
    }

    public void llenarTabla()
    {
        this.canales = this.util.traerTodos(jTableCanales, canales);
    }

    private void centrar()
    {
        Dimension pantalla, cuadro;
    	pantalla = Toolkit.getDefaultToolkit().getScreenSize();
        cuadro = this.getSize();
    	this.setLocation(((pantalla.width - cuadro.width)/2), (pantalla.height - cuadro.height)/2);
    }


    private void ajustarTamanioColumna()
    {
        TableColumn column = null;
        for (int i = 0; i < 4; i++)
        {
            column = jTableCanales.getColumnModel().getColumn(i);
            switch(i)
            {
                case 0:
                        column.setPreferredWidth(25);
                        break;
                case 1:
                        column.setPreferredWidth(295);
                        break;
                case 2:
                        column.setPreferredWidth(20);
                        break;
                case 3:
                        column.setPreferredWidth(15);
                        break;
            }
        }
    }

    private void agregarCanal()
    {
        this.util.agregarCanalesAPerfil(nuevoPerfil, jTableCanales, canales);
    }

    /**
     * Metodo para limpiar los registros de la tabla.
     * Quita todas las filas de la tabla.
     */
    private void limpiar()
    {
        DefaultTableModel modelo = (DefaultTableModel)this.jTableCanales.getModel();
        while(this.jTableCanales.getRowCount() != 0)
                modelo.removeRow(0);
        modelo = null;
    }


//
//    private Canal buscarCanal(int fila)
//    {
//        Canal ca = new Canal();
//        Iterator it = this.canales.iterator();
//        while(it.hasNext())
//        {
//            ca = (Canal)it.next();
//            if(ca.getCodigo() == Integer.parseInt(String.valueOf(this.jTableCanales.getValueAt(fila, 0))))
//                break;
//        }
//        return ca;
//    }
}
